package model;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;


import java.util.ArrayList;

import controller.Main;


public class File {

	/**
	 * Read a file and put information into arraylist
	 */
	 public static void read() {
		 String csvFile = "ContactList.csv";
			BufferedReader br = null;
			String line = "";
			String cvsSplitBy = ";";
			
			try {
				 
				br = new BufferedReader(new FileReader(csvFile));
				while ((line = br.readLine()) != null) {
		 
				        // use comma as separator
					String[] information = line.split(cvsSplitBy);
					Contact c = new Contact(information[0],information[1],information[2],information[3],information[4],information[5],information[6],information[7],information[8]);
					Main.tab.add(c);
		 
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (br != null) {
					try {
						br.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		 
			System.out.println("Done");
		  }
	
	 /**
	  * Write in a CSV file
	 * @throws IOException 
	  */
	 
	 public static void write(String firstName, String lastName, String homeAddress, String companyAddress, String personalPhone, String homePage, String email, String photo, String group) throws IOException{
		   FileWriter fw = new FileWriter("ContactList.csv", true);
		   PrintWriter out = new PrintWriter(fw);
		   out.print("\n" + firstName + " " + ";");
		   out.print(lastName + " " + ";");
		   out.print(homeAddress + " " + ";");
		   out.print(companyAddress + " " + ";");
		   out.print(personalPhone + " " + ";");
		   out.print(homePage + " " + ";");
		   out.print(email + " " + ";");
		   out.print(photo + " " + ";");
		   out.print(group + " " + ";");
		   
		      
		   //Flush the output to the file
		   
		   out.flush();
		       
		   //Close the Print Writer
		   out.close();
		       
		   //Close the File Writer
		   fw.close();       
		  }
	 
	 public static void delete(){
		 
	 }

	 public static void writeAll() throws IOException{

		 
		   FileWriter fw = new FileWriter("ContactList.csv", true);		
		   PrintWriter out = new PrintWriter(fw);
		   
		   for(int i=0;i<Main.tab.size();i++)
		   {
		   out.print("\n" + Main.tab.get(i).getFirstName() + " " + ";");
		   out.print(Main.tab.get(i).getLastName() + " " + ";");
		   out.print(Main.tab.get(i).getHomeAddress() + " " + ";");
		   out.print(Main.tab.get(i).getCompanyAddress() + " " + ";");
		   out.print(Main.tab.get(i).getPersonalPhone() + " " + ";");
		   out.print(Main.tab.get(i).getHomePage() + " " + ";");
		   out.print(Main.tab.get(i).getEmail() + " " + ";");
		   out.print(Main.tab.get(i).getPhoto() + " " + ";");
		   out.print(Main.tab.get(i).getGroup() + " " + ";");
		   }
		      
		   //Flush the output to the file
		   
		   out.flush();
		       
		   //Close the Print Writer
		   out.close();
		       
		   //Close the File Writer
		   fw.close();       
		  }

	
	}
	 

	 
